<script setup lang="ts">
	import { useStore, useCool } from "/@/cool";
	import { storage } from "/@/cool/utils";
	import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
	import { useUi } from "/$/cool-ui";
	const ui = useUi()
	const { router } = useCool();
	onLaunch(async () => {
		console.log("App Launch");
		const { dict, user } = useStore();
		if (user.token) {
			// 获取登录用户信息
			user.get();
		}
		// 获取字典
		await dict.refresh();
		// #ifdef MP-WEIXIN
		const updateManager = uni.getUpdateManager();
		updateManager.onUpdateReady(function () {
			updateManager.applyUpdate()
		})
		// #endif

		// #ifdef APP
		// 监听点击菜单栏消息
		void plus.push.addEventListener('click', (e : any) => {
			router.push(dict.getValue('push', e?.payload?.type) + "?id=" + e.payload.id)
		}, false)
		uni.getPushClientId({
			success: async (res) => {
				// console.log(res)
				if (storage.get("userInfo")) {
					let { clientId } = storage.get("userInfo")
					if (res.cid != clientId) {
						user.update({ clientId: res.cid })
					}
					console.log(clientId)
				}
			},
			fail: async (err) => {
				console.log(err)
			}
		})
		uni.onPushMessage(async (res) => {
			let { data, type } = res
			if (type !== 'click') {
				console.log(storage.get('vibrate'),"qeqeqwe")
				if (storage.get('vibrate')) {
					uni.vibrateShort()
				}
				ui.showPush({ title: data.title, content: data.content, payload: data.payload, path: dict.getValue('push', data?.payload?.type) })
				console.log("收到推送消息:=", data) //监听推送消息
			}
		})
		// #endif
	});

	onShow(() => {
		console.log("App Show");
		//#ifdef APP-PLUS
		var args = plus.runtime.arguments;
		if (args) {
			let path = args.split("//")
			if (path.length > 1) {
				router.push('/' + path[1])
			} else {
				router.push(args)
			}
			plus.runtime.arguments = null;
			plus.runtime.arguments = "";
		}
		//#endif
	});

	onHide(() => {
		let callback = (res) => {
			console.log(res)
		}
		uni.offPushMessage(callback);
		console.log("App Hide");
	});
</script>

<style lang="scss">
	@import "/static/font/iconfont.css";
	@import "/static/css/iconfont/index.scss";
	@import "/$/cool-ui/index.scss";
	@import "/@/static/css/index.scss";
	@import '@climblee/uv-ui/index.scss';

	page {
		font-family: Helvetica Neue, Helvetica, Arial;
	}

	// #ifdef WEB
	uni-page-head {
		display: none !important
	}

	//  #endif
</style>